using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class BrushItem : BaseItem
	{
		public enum State
		{
			Default = 0,
			Up = 1
		}

		public enum AnimState
		{
			Static = 0,
			Up = 1,
			Down = 2
		}

		private const float MOVE_SPEED = 1.6f;

		public State state;

		public Color32 paintColor;

		private AnimState animState;

		private float originalY;

		private float upY;

		private float curY;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			state = (State)data.state;
			paintColor = data.color;
			originalY = model.localPosition.y;
			upY = originalY + 0.2f;
			if (state == State.Up)
			{
				curY = upY;
				model.localPosition = new Vector3(0f, curY, 0f);
			}
			else
			{
				curY = originalY;
			}
		}

		protected override void LateUpdate()
		{
			if (animState == AnimState.Static)
			{
				return;
			}
			if (animState == AnimState.Up)
			{
				curY += Time.deltaTime * 1.6f;
				if (curY >= upY)
				{
					curY = upY;
					animState = AnimState.Static;
				}
			}
			else if (animState == AnimState.Down)
			{
				curY -= Time.deltaTime * 1.6f;
				if (curY <= originalY)
				{
					curY = originalY;
					animState = AnimState.Static;
				}
			}
			model.localPosition = new Vector3(0f, curY, 0f);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			Singleton<AudioManager>.Instance.PlaySound("sound_select_paint");
			GlobalEventSystem.Fire(new BaseEvent("game.change_brush", archiveData.itemData.id));
		}

		public void SetState(State val)
		{
			if (state != val)
			{
				if (state == State.Default)
				{
					animState = AnimState.Up;
				}
				else
				{
					animState = AnimState.Down;
				}
				state = val;
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
			obj["30"] = paintColor.r;
			obj["31"] = paintColor.g;
			obj["32"] = paintColor.b;
		}
	}
}
